﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Net;



namespace MKIB3Game
    {
    public class Sender
        {
            private TcpClient client;
            private Thread senderThread;
            private List<string> messageBuffer;
            public bool MAINLOOP = true;

            public Sender(TcpClient client)
            {
               this.client = client;
               messageBuffer = new List<string>();
               this.initThread();
            }
            private void initThread()
            {
                senderThread = new Thread(sendThread);
                senderThread.Name = "Sender Thread";
                senderThread.Start();
            }
            public int getBufferSize()
            {
                return messageBuffer.Count;
            }

            private string getMessage()
            {
                if (messageBuffer.Count != 0)
                {
                    string ret = (string)messageBuffer[0];
                    messageBuffer.RemoveAt(0);
                    return ret;
                }
                return null;
            }




        
            public void send(String message)
            {
                messageBuffer.Add(message+"\r");
            }

          private void sendThread(){
                            Console.WriteLine(Thread.CurrentThread.Name + " ist the current thread");
                        try
                        {                            
                            string sendmessage = null; 
                            NetworkStream stream = client.GetStream();
                            if (stream.CanWrite)
                            {
                                    while (MAINLOOP)
                                    {
                                        sendmessage = this.getMessage();
                                        if (sendmessage != null)
                                        {
                                                Console.WriteLine("sending...");
                                         Byte[] sendBytes = Encoding.UTF8.GetBytes(sendmessage);
                                         stream.Write(sendBytes, 0, sendBytes.Length);
                                         stream.Flush();                            
                                                 Console.WriteLine("...sent. ->>  " + sendmessage);
                                        }
                                        Thread.Sleep(10);// test
                                    }// ende while
                            }//end if can write
                            else
                            {
                                Console.WriteLine("writing to stream not possible");
                            }
                        }//ende try
                        catch (InvalidOperationException e)
                        {
                            Console.WriteLine(e.Message + "\n");
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message + "\n");
                        } 
            }//end send 
        }//end class
    }
 
